/*
 This file 'TextField' is part of Firebird Integrated Solution 1.0

 Copyright (c) 2014 Lincong

 Contact:  
 Email: lincong1987@gmail.com

 QQ: 159257119

 See Usage at http://www.jplatformx.com/firebird

 Create date: 2014-06-19 15:57
 */

define(function (require, exports, module) {

	var Component = require("Firebird.ui.Component");

	/**
	 * Firebird.ui.TextField
	 * @class Firebird.ui.TextField
	 */
	Firebird.define("Firebird.ui.TextField", {
		extend: "Firebird.ui.Component",

		statics: {
			_uuid: 1,
			uuid: function () {
				return this._uuid++;
			},
			/**
			 * 属性适配器
			 * @param $component
			 * @returns {{}}
			 */
			attrs: function ($component) {

				return $.extend({}, {
					renderTo: $component.get(0),
					instanceId: $component.attr("fb-instance") || $component.attr("name") || $component.attr("id") || ("textfield_" + Firebird.ui.TextField.uuid()),
					width: $component.attr("fb-width"),
					//value: $component.attr("fb-value") || $component.val(),
					placeholder: $component.attr("fb-placeholder") || $component.attr("placeholder") || "",

					limit: Firebird.ui.Component.eval($component.attr("fb-limit")),
					on: Firebird.ui.Component.eval($component.attr("fb-on")),

					focus: Firebird.ui.Component.eval($component.attr("fb-focus")),
					disabled: Firebird.ui.Component.eval($component.attr("fb-disabled") || $component.prop("disabled")),
					readonly: Firebird.ui.Component.eval($component.attr("fb-readonly") || $component.prop("readonly")),
					hidden: Firebird.ui.Component.eval($component.attr("fb-hidden") || $component.is("[type=hidden]"))
				});
			}
		},

		config: {
			renderTo: null,
			instanceId: null,
			width: 222,
			placeholder: null,
			//value: "",

			limit: null,
			on: null,
			keyup: null,
			keypress: null,
			keydown: null,
			disabled: false,
			focus: false,
			readonly: false,
			hidden: false
		},
		/**
		 * 限制输入
		 */
		limit: null,
		limitRules: {
			number: {
				reg: /\s/,
				ime: true,
				trigger: "keydown keypress"
			},
			chinese: {
				reg: /\s/,
				ime: true,
				trigger: "keydown keypress"
			},
			length: {
				reg: /\s/,
				ime: true,
				trigger: "keydown keypress"
			}
		},

		instance: null,
		wrapper: null,

		isPlaceholderSupport: "placeholder" in document.createElement("input"),

		wrapCls: "fb-ui-textfield",
		_init: function (config) {
			var me = this;

			me.instance = $(me.getRenderTo()).addClass(this.wrapCls + "-input").data({
				instance: me,
				xtype: "textfield"
			}).css("box-sizing", "border-box");

			this.render();

			this.setReadonly(this.getReadonly());
			this.setDisabled(this.getDisabled());
			this.setWidth(this.getWidth());
			this.setFocus(this.getFocus());
			this.setHidden(this.getHidden());
			//if (!isPlaceholderSupport) {
			this.setPlaceholder(this.getPlaceholder());
			//}

			me.setValue(me.instance.attr("fb-value") || me.instance.val());

			this.bindPlaceholder();
		},

		render: function () {
			var me = this;
			me.instance.wrap($("<span class='" + this.wrapCls + "'></span>"));
			var wrapper = this.wrapper = me.instance.parent();

			me.wrapper.data("instance", me);
			var $placeholder = me.$placeholder = $("<span class='" + this.wrapCls + "-placeholder'></span>");
			me.instance.before($placeholder);

			me.instance.on("focus", function () {
				wrapper.addClass(me.wrapCls + "-focus");
				setTimeout(function () {
					me.bindPlaceholder();
					me.instance.trigger("focusout");
				}, 0);
				me.instance.trigger("focusin");
				me.instance.trigger("onFocus");
			}).on("blur", function () {
				wrapper.removeClass(me.wrapCls + "-focus");
				me.bindPlaceholder();
				me.instance.trigger("onBlur");
			}).on("keydown keyup keypress change", function () {
				me.bindPlaceholder();
			});

			this.wrapper.on("mouseenter", function () {
				me.wrapper.addClass(me.wrapCls + "-hover");
				if (me.wrapper.hasClass(me.wrapCls + "-invalid")) {
					me.instance.trigger("showErrorMessage");
				}
			});

			this.wrapper.on("mouseleave", function () {
				me.wrapper.removeClass(me.wrapCls + "-hover");
				if (me.wrapper.hasClass(me.wrapCls + "-invalid")) {
					me.instance.trigger("hideErrorMessage");
				}
			});

			this.$placeholder.on("click", function () {
				me.setFocus(true);
			});

		},

		on: function () {
			this.instance.on.apply(this.instance, arguments);
		},

		applyFocus: function (status) {
			var me = this;
			if (this.instance) {
				if (status === true) {
					setTimeout(function () {
						me.instance.focus();
					}, 0);
				}
			}
			return status;
		},
		widthTimer: 0,
		applyWidth: function (width) {
			var me = this;
			if (this.wrapper) {
				me.wrapper.width(width);
			}
			return width;
		},

		applyValue: function (val) {
			if (this.instance) {
				this.instance.val(val);
			}
			return val;
		},

		applyHidden: function (status) {
			if (this.wrapper) {
				var status = !!status;
				this.wrapper.css({display: status ? "none" : ""});
				this._hidden = status;
			}
			return status;
		},

		applyDisabled: function (status) {
			if (this.instance) {
				var status = !!status;
				this.instance.prop("disabled", status);
				this.wrapper[status == true ? "addClass" : "removeClass"](this.wrapCls + "-disabled");
				this._disabled = status;
				this.setTitle();
			}
			return status;
		},

		applyReadonly: function (status) {
			if (this.instance) {
				var status = !!status;
				this.instance.prop("readonly", status);
				this.wrapper[status == true ? "addClass" : "removeClass"](this.wrapCls + "-readonly");
				this._readonly = status;
				this.setTitle();
			}
			return status;
		},

		applyPlaceholder: function (text) {
			if (this.$placeholder) {
				if (!this.isPlaceholderSupport) {
					this.$placeholder.html(text);
				} else {
					this.instance.attr({
						placeholder: text
					});
				}
			}
			return text;
		},

		showPlaceholder: function () {
			this.$placeholder.show(0);
		},

		hidePlaceholder: function () {
			this.$placeholder.hide(0);
		},

		bindPlaceholder: function () {
			if (this.isPlaceholderSupport) {
				return;
			}
			if (this.instance.val() == "") {
				this.showPlaceholder();
			} else {
				this.hidePlaceholder();
			}
		},

		setTitle: function () {
			var title = "readonly disabled".replace(/readonly/gi, this._readonly == true ? "只读" : "")
				.replace(/disabled/gi, this._disabled == true ? "禁用" : "");
			this.wrapper.attr("title", title);
			this.instance.attr("title", title);
		},

		_isDefaultRule: function (rule) {
			return $.grep(this.limitRules, function (v, k) {
				return k == rule;
			});
		},
		bindLimitEvent: function () {
			// limit is an array
			// like ["number", {}, {}]
			var me = this;
			$.each(me.limit, function (i, rule) {
				if (typeof (rule) === "string" && me._isDefaultRule(rule) != null) {

				} else if (rule instanceof RegExp) {

				} else {

				}
			});

			this.handleEvent();
		},
		handleEvent: function () {
			var ctrlKey = null;
			this.css('ime-mode', 'disabled').on('keydown', function (e) {
				ctrlKey = e.ctrlKey;
			}).on('keypress', function (e) {
				var allowedReg = typeof allowedReg == 'string' ? new RegExp(allowedReg) : allowedReg;
				var charCode = typeof e.charCode != 'undefined' ? e.charCode : e.keyCode;
				var keyChar = $.trim(String.fromCharCode(charCode));
				if (!ctrlKey && charCode != 0 && charCode != 13 && !allowedReg.test(keyChar)) {
					e.preventDefault();
				}
			});
		},
		getValue: function () {
			return this.instance.val();
		},
		setValue: function (val) {
			this.instance.val(val);
			this.bindPlaceholder();
			return this;
		},
		clear: function () {
			this.setValue("");
			return this;
		}
	});

	module.exports = Firebird.ui.TextField;

});